package com.infolab.aims.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;


import com.infolab.aims.common.AbstractController;
import com.infolab.aims.common.bean.SearchCriteria;
import com.infolab.aims.common.bean.SearchResult;
import com.infolab.aims.common.exception.ResourceNotFoundException;
import com.infolab.aims.domain.Department;
import com.infolab.aims.domain.Section;
import com.infolab.aims.service.intf.SectionService;


@Controller
@RequestMapping("/section")
@SessionAttributes(value=SectionController.SESSION_SEARCH_CRITERIA)
public class SectionController extends AbstractController {
	
	public static final String SESSION_SEARCH_CRITERIA ="searchCriteria";
	public static final String CONTEXT_PATH ="/section";
	public static final String JSP_MAIN_FORM="sectionForm";
	public static final String JSP_MAIN_SEARCH="sectionSearch";

	@Autowired
	SectionService sectionService;

	@RequestMapping(value="create", method = RequestMethod.GET)
	public String createSection(Model model) {
		
		model.addAttribute(new Section());
		return JSP_MAIN_FORM;
	}
	
	@RequestMapping(value="create",method=RequestMethod.POST)
	public String saveSection(@ModelAttribute Section section, BindingResult result,Model model,HttpServletRequest request) {

		if (validateSection(section, result)) {	
			return JSP_MAIN_FORM;
		}
		
		section.setCreatedBy(getCurrentUserName(request));
		
		prepareAuditObject(section, request);
		sectionService.createSection(section);
		
		setSuccessMessage(getMessageResources("success.create","section " + section.getSectionName()));
		
		refreshGlobalSection(request);
		
		return "redirect:"+CONTEXT_PATH+"/update/" + section.getSectionId();
		

	}
	
	@RequestMapping(value="update/{id}",method=RequestMethod.POST)
	public String updateSection(@PathVariable Integer id,@ModelAttribute Section section, BindingResult result,Model model,HttpServletRequest request) {

		if (validateSection(section, result)) {
			return JSP_MAIN_FORM;
		}
		
		section.setModifiedBy(getCurrentUserName(request));
		
		prepareAuditObject(section, request);
		sectionService.updateSection(section);
		
		setSuccessMessage(getMessageResources("success.update","section " + section.getSectionName()));
		
		refreshGlobalSection(request);
		
		return "redirect:"+CONTEXT_PATH+"/update/" + section.getSectionId();	
		
	}
	
	@RequestMapping(value={"update/{id}","detail/{id}"}, method = RequestMethod.GET)
	public String detailSection(@PathVariable Integer id,Model model,HttpServletRequest request) {
		
		Section section = sectionService.getSection(id);
		
		if(section==null) {
			throw new ResourceNotFoundException("Section " + id);
		}
		
		model.addAttribute(section);
		return JSP_MAIN_FORM;

	}
	

	
	@ModelAttribute(SectionController.SESSION_SEARCH_CRITERIA)
	public SearchCriteria getSectionSearchCriteria()
	{
		return new SearchCriteria();
	}
	

	@RequestMapping(value="search")
	public String searchSection(@ModelAttribute(SectionController.SESSION_SEARCH_CRITERIA) SearchCriteria searchCriteria,@ModelAttribute("currentSearchCriteria") SearchCriteria currentSearchCriteria,Model model) {
		
		if(currentSearchCriteria.getCurPage()!=null)
			searchCriteria.setCurPage(currentSearchCriteria.getCurPage());
		
		searchCriteria.addSearchCriteria("active", true);
		
		SearchResult searchResult = sectionService.searchSection(searchCriteria);
		model.addAttribute(searchResult);
		
		return JSP_MAIN_SEARCH;
	}
	
	@RequestMapping(value={"delete/{id}"}, method = RequestMethod.GET)
	public String deleteSection(@PathVariable Integer id,Model model,HttpServletRequest request) {
		
		Section section = sectionService.getSection(id);
		section.setModifiedBy(getCurrentUserName(request));
		
		prepareAuditObject(section, request);
		sectionService.deleteSection(section);
		
		refreshGlobalSection(request);
		
		return "redirect:"+CONTEXT_PATH+"/search";
	}
	
	
	private boolean validateSection(Section section,BindingResult result)
	{
		if(section.getDepartment().getDepartmentId()==null)
			section.setDepartment(null);
		
		validator.validate(section, result);
		
		return result.hasErrors();
	}
	
	private void prepareAuditObject(Section section, HttpServletRequest request){
		/* audit start */
		Department department = findDepartment(request, section.getDepartment().getDepartmentId());
		section.setDepartment(department);		
		/* audit end */	
	}

}
